<?php
class Member_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('user_info');

        return $query;
    }

    public function create_member()
    {
        $username = $this->input->post('reg_username');
        $password = $this->input->post('reg_pw');
        $email    = $this->input->post('reg_email');

        if(empty($username)|| empty($password) || empty($email))
            die('Required fields must filled.
                <br/>
                <a href="' . site_url('login/reg') . '">Redirect in 10 seconds.</a>
                <script type="text/javascript">
                setTimeout(function() {
                    window.location = "' . site_url('login/reg') . '"
                }, 10000);
                </script>;
                ');
        
        //Some validate.
        //TODO

        $new_member_insert_data = array(
            'username' => $username,
            'password' => md5($password),
            'email' => $email,
            'first_name' => $this->input->post('reg_first_name'),
            'last_name' => $this->input->post('reg_last_name'),
            'age' => $this->input->post('reg_age'),
            'school' => $this->input->post('reg_school')
        );

        $insert = $this->db->insert('user_info', $new_member_insert_data);
        return $insert;
    }

    public function approve_user($id)
    {
        $this->db->query("UPDATE `user_info` SET `approved` = 1 WHERE `id` = '$id' LIMIT 1");
        if($this->db->affected_rows())
        {
            return true;
        }else{
            return false;
        }
    }

    public function remove_user($id)
    {
        $this->db->query("DELETE FROM `user_info` WHERE `id`= '$id' LIMIT 1;");
        if($this->db->affected_rows())
        {
            return true;
        }else{
            return false;
        }
    }
}